# UpgradeOptionButton.gd
extends PanelContainer # Or another container/control node

signal upgrade_selected(upgrade_resource: Resource)

@onready var title_label: Label = $MarginContainer/VBoxContainer/TitleLabel
@onready var description_label: Label = $MarginContainer/VBoxContainer/DescriptionLabel
@onready var icon_rect: TextureRect = $MarginContainer/VBoxContainer/HBoxContainer/Icon # Optional Icon
@onready var select_button: Button = $MarginContainer/VBoxContainer/SelectButton

var current_upgrade: Resource = null

func _ready():
	select_button.pressed.connect(_on_select_button_pressed)


func set_upgrade_data(upgrade_res: Resource):
	current_upgrade = upgrade_res
	if not current_upgrade:
		title_label.text = "Error"
		description_label.text = "Invalid Upgrade Data"
		select_button.disabled = true
		return

	select_button.disabled = false

	# Assume PlayerStatUpgrade and WeaponUpgrade have 'description' and potentially 'icon'
	# You might need more specific naming or structure
	var title = "Upgrade"
	if current_upgrade.has("stat_name"): # Player Stat Upgrade
		title = current_upgrade.stat_name.capitalize()
	elif current_upgrade.has("weapon_id"): # Weapon Upgrade
		title = "%s: %s" % [current_upgrade.weapon_id.capitalize(), current_upgrade.upgrade_type.capitalize()]
		if current_upgrade.has("is_new_weapon") and current_upgrade.is_new_weapon:
			title = "New Weapon: %s" % current_upgrade.weapon_id.capitalize()

	title_label.text = title
	description_label.text = current_upgrade.description if current_upgrade.has("description") else "No description."

	if icon_rect and current_upgrade.has("icon") and current_upgrade.icon:
		icon_rect.texture = current_upgrade.icon
		icon_rect.visible = true
	elif icon_rect:
		icon_rect.visible = false


func _on_select_button_pressed():
	if current_upgrade:
		emit_signal("upgrade_selected", current_upgrade)
	else:
		printerr("No upgrade resource associated with this button.")